jquery synchronous AJAX request

How to make synchronous ajax request using jquery

Bydefault jquery ajax request is asynchronouse means execution of next code is continue whether or not your ajax request is complete or not.

Create synchronous ajax request:

For creating sync request you can use property async as false

Cross-domain requests anddataType: “jsonp” requests do not support synchronous operation. Note that synchronous requests may temporarily lock the browser, disabling any actions while the request is active.

     

    var $n = jQuery.noConflict(); //prevents jquery conflict

     $n().ready(function() { 
     
     $n("#btn_postdata").click(function() {
   
           var str = "name=nikunj&age=22"; //pass post data to server.php  
           $n.ajax({
                       type: "POST", //send data using post method
                       url: "server.php", //this is post data to server.php
                       data: str,
                       async:false,//this will make your ajax request synchronous.
                       success: function(msg){
                       alert(msg);    //this is return data from server.php
                      }

               });
           
       });      

    });